package com.ruoyi.web.controller.ycdj;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("meeting")
@Api(tags = "会议")
public class MeetingController extends BaseController {

    @Autowired
    public TMeetingtypeService tMeetingtypeService;

    @Autowired
    public TConfigService tConfigService;

    @Autowired
    public TMeetingService tMeetingService;

    @Autowired
    public TOrganizationService tOrganizationService;

    @Autowired
    public TUserService tUserService;

    //列表
    @ApiOperation("类型列表")
    @GetMapping("gettypelist")
    public AjaxResult gettypelist(){
        //获取党组织类别
        QueryWrapper<TConfig>tConfigQueryWrapper=new QueryWrapper<>();
        tConfigQueryWrapper.eq("configflag","dangzuzhileibie");
        List<TConfig>tConfigList=tConfigService.list(tConfigQueryWrapper);
        if(!tConfigList.isEmpty()){
            for(TConfig tConfig:tConfigList){
                tConfig.setLabel_id("p_"+tConfig.getId());
                QueryWrapper<TMeetingtype>tMeetingtypeQueryWrapper=new QueryWrapper<>();
                tMeetingtypeQueryWrapper.eq("pid",tConfig.getId());

                List<TMeetingtype>tMeetingtypeList=tMeetingtypeService.list(tMeetingtypeQueryWrapper);
                if(!tMeetingtypeList.isEmpty()){
                    for(TMeetingtype tMeetingtype:tMeetingtypeList){
                        tMeetingtype.setLabel_id("c_"+tMeetingtype.getId());
                    }
                    tConfig.setChildren(tMeetingtypeList);
                }else{
                    tConfig.setChildren(new ArrayList<>());
                }
            }

            return AjaxResult.success(tConfigList);

        }else{
            return AjaxResult.warn("请先配置党组织类型！");
        }
    }

    //新增类型
    @ApiOperation("新增类型")
    @PostMapping("add")
    public AjaxResult addtype(@RequestBody TMeetingtype tMeetingtype){
        return toAjax(tMeetingtypeService.save(tMeetingtype));
    }

    //编辑类型
    @ApiOperation("编辑类型")
    @PostMapping("edittype")
    public AjaxResult edittype(@RequestBody TMeetingtype tMeetingtype){
        return toAjax(tMeetingtypeService.saveOrUpdate(tMeetingtype));
    }

    //修改状态
    @ApiOperation("修改状态")
    @GetMapping("edittypestatus")
    public AjaxResult edittypestatus(Integer id){
        TMeetingtype tMeetingtype=tMeetingtypeService.getById(id);
        if(tMeetingtype.getStatus()==1) {
            tMeetingtype.setStatus(0);
        }else{
            tMeetingtype.setStatus(1);
        }
        return toAjax(tMeetingtypeService.saveOrUpdate(tMeetingtype));
    }

    //获取当前登录人的会议类型
    @ApiOperation("获取登录人对应会议类型")
    @GetMapping("getusermeettypelist")
    public AjaxResult getusermeettypelist(){
        SysUser user=SecurityUtils.getLoginUser().getUser();
        QueryWrapper<TUser>tUserQueryWrapper=new QueryWrapper<>();
        tUserQueryWrapper.eq("sysuserid",user.getUserId());
        TUser tUser=tUserService.getOne(tUserQueryWrapper);
        QueryWrapper<TOrganization>tOrganizationQueryWrapper=new QueryWrapper<>();
        tOrganizationQueryWrapper.eq("sysdeptid", tUser.getDeptid());
        TOrganization tOrganization=tOrganizationService.getOne(tOrganizationQueryWrapper);
        QueryWrapper<TMeetingtype>tMeetingtypeQueryWrapper=new QueryWrapper<>();
        tMeetingtypeQueryWrapper.eq("pid",tOrganization.getTypeid());
        List<TMeetingtype>tMeetingtypeList=tMeetingtypeService.list(tMeetingtypeQueryWrapper);
        return AjaxResult.success(tMeetingtypeList);
    }

    //会议列表
    @ApiOperation("会议列表")
    @GetMapping("getmeetinglist")
    public AjaxResult getmeetinglist(@RequestParam(value="pageNum",defaultValue = "")Integer pageNum,
                                     @RequestParam(value="pageSize",defaultValue = "")Integer pageSize) {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            QueryWrapper<TOrganization> tOrganizationQueryWrapper = new QueryWrapper<>();
            tOrganizationQueryWrapper.eq("sysdeptid", user.getDeptId());
            TOrganization tOrganization = tOrganizationService.getOne(tOrganizationQueryWrapper);
            QueryWrapper<TMeeting> tMeetingQueryWrapper = new QueryWrapper<>();
            tMeetingQueryWrapper.eq("orgid", tOrganization.getId());

        String meettypename="";
        if(pageNum!=null){
            PageHelper.startPage(pageNum,pageSize);

            List<TMeeting>tMeetingList=tMeetingService.list(tMeetingQueryWrapper);

            for(TMeeting tMeeting:tMeetingList){
                tMeeting.setOrgname(tOrganizationService.getById(tMeeting.getOrgid()).getName());
                //拆分类型，按名称返回
                if(tMeeting.getMeetingtype().contains(",")){
                    meettypename="";
                   for(int i=0;i<tMeeting.getMeetingtype().split(",").length;i++){
                       meettypename+=tMeetingtypeService.getById(tMeeting.getMeetingtype().split(",")[i]).getTypename()+",";
                   }
                   tMeeting.setMeetingtypename(meettypename.substring(0,meettypename.length()-1));
                }else{
                    tMeeting.setMeetingtypename(tMeetingtypeService.getById(tMeeting.getMeetingtype()).getTypename());
                }

                //发起人
                tMeeting.setCreatername(tUserService.getById(tMeeting.getCreaterid()).getUsername());
            }
            PageInfo<TMeeting>pageInfo=new PageInfo<>(tMeetingList);
            return AjaxResult.success(pageInfo);
        }else{
            List<TMeeting>tMeetingList=tMeetingService.list(tMeetingQueryWrapper);
            for(TMeeting tMeeting:tMeetingList){
                tMeeting.setOrgname(tOrganizationService.getById(tMeeting.getOrgid()).getName());
                //拆分类型，按名称返回
                if(tMeeting.getMeetingtype().contains(",")){
                    meettypename="";
                    for(int i=0;i<tMeeting.getMeetingtype().split(",").length;i++){
                        meettypename+=tMeetingtypeService.getById(tMeeting.getMeetingtype().split(",")[i]).getTypename()+",";
                    }
                    tMeeting.setMeetingtypename(meettypename.substring(0,meettypename.length()-1));
                }else{
                    tMeeting.setMeetingtypename(tMeetingtypeService.getById(tMeeting.getMeetingtype()).getTypename());
                }

                //发起人
                tMeeting.setCreatername(tUserService.getById(tMeeting.getCreaterid()).getUsername());
            }
            return AjaxResult.success(tMeetingList);
        }

    }

    //新增会议
    @ApiOperation("新增会议")
    @PostMapping("addmeeting")
    public AjaxResult addmeeting(@RequestBody TMeeting tMeeting){
        QueryWrapper<TUser>tUserQueryWrapper=new QueryWrapper<>();
        tUserQueryWrapper.eq("sysuserid",SecurityUtils.getUserId());
        tMeeting.setCreaterid(tUserService.getOne(tUserQueryWrapper).getId());
        return toAjax(tMeetingService.save(tMeeting));
    }

    //编辑会议
    @ApiOperation("编辑会议")
    @PostMapping("editmeeting")
    public AjaxResult editmeeting(@RequestBody TMeeting tMeeting){
        return toAjax(tMeetingService.saveOrUpdate(tMeeting));
    }


}
